package cn.promonkey.api.permission.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.promonkey.core.response.ResultMsg;
import cn.promonkey.core.toolkit.QueryGenerator;
import cn.promonkey.api.permission.entity.Permission;
import cn.promonkey.api.permission.service.PermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;

/** @author <no value> */
@Slf4j
@RestController
@RequestMapping("/permission/")
public class PermissionController {
  final PermissionService permissionService;

  public PermissionController(PermissionService permissionService) {
    this.permissionService = permissionService;
  }

  @RequestMapping("getPage")
  public String getPage(
      Permission permission,
      @RequestParam(name = "page", defaultValue = "1") Integer page,
      @RequestParam(name = "size", defaultValue = "10") Integer size,
      HttpServletRequest request) {
    QueryWrapper<Permission> queryWrapper = QueryGenerator.initQueryWrapper(permission, request.getParameterMap());
    Page<Permission> pageable = new Page<>(page, size);
    IPage<Permission> pageList = permissionService.page(pageable, queryWrapper);
    return ResultMsg.success(pageList);
  }

  @RequestMapping(value = "save", method = RequestMethod.POST)
  public String save(@RequestBody Permission permission) {
    return ResultMsg.success(permissionService.save(permission));
  }

  @RequestMapping(value = "edit", method = RequestMethod.POST)
  public String edit(@RequestBody Permission permission) {
    return ResultMsg.success(permissionService.updateById(permission));
  }

  @RequestMapping(value = "get")
  public String get(@RequestParam(name = "id") Long id) {
    return ResultMsg.success(permissionService.getById(id));
  }

  @RequestMapping(value = "delete", method = RequestMethod.POST)
  public String delete(@RequestBody Long id) {
    return ResultMsg.success(permissionService.removeById(id));
  }
}
